local skynet = require "skynet"
local def = require "srv_define"
local TableLogic = require "game_logic"
local log = require "sky_log"

local robot = {}
local player_num = 0
local base_note = 0
local control_mode = 0                      --0正常 -1回收 1赠送
local the_robot = {}

function robot.init(players, base, mode)
    player_num = players
    base_note = base
    control_mode = mode
end

function robot.debuInfo()
    log.info("------the_robot=", the_robot)
    log.info("------idlist=", id_list)
end

function robot.game_start(seatList)
    for _, v in pairs(seatList) do
        the_robot[v.seat_id] = {}
        the_robot[v.seat_id].seat_id = v.seat_id
        the_robot[v.seat_id].player_id = v.player_id
        the_robot[v.seat_id].isRobot = v.isRobot
    end
    log.debug("the_robot=", the_robot)
end

--机器人需要打一张牌
function robot.needOutcard(func, seatId, handCards, tingCards, lastGetCard, playerHucards)
    local args = {
        seat_id = seatId,
        oper_type = def.operType.outCard,      
        oper_cards = TableLogic.GetBestOutCard(handCards, tingCards, lastGetCard, true, playerHucards),
    }

    log.info("robot do needOutcard！", args)
    
    local oper_time = math.random(1, 4) * 100
    skynet.timeout(100, function()
        func(args)
    end)  
end

--机器人需要选择操作
function robot.canOper(func, seatId, handCards, combos, tingCards, lastGetCard)
    log.info("robot.canOper combos=", combos)
    --local oneAction = combos[math.random(1, #combos)]
    local oneAction = {
        oper_type = def.operType.check,
        cards = {},
    }

    --combos排下优先级
    table.sort(combos, function (a, b) return a.oper_type>b.oper_type end )
    --log.info("robot.canOper combos=", combos)

    for _, v in ipairs(combos) do
        --杠以上可以直接操作的
        if v.oper_type>=def.operType.mingGang then 
            oneAction.oper_type = v.oper_type
            oneAction.cards = v.cards
            break
        end
        --碰判断下，自己是否已有刻子，还有该碰的2张牌，前后是否成组
        if v.oper_type == def.operType.peng then 
            local isPeng = true
            if handCards[v.cards[1]] == 3 then 
                isPeng = false
            end
            local one = (handCards[v.cards[1]-1]~=nil and handCards[v.cards[1]-1]==2) and  (handCards[v.cards[1]-2]~=nil and handCards[v.cards[1]-2]==2)
            local two = (handCards[v.cards[1]-1]~=nil and handCards[v.cards[1]-1]==2) and  (handCards[v.cards[1]+1]~=nil and handCards[v.cards[1]+1]==2)
            local three = (handCards[v.cards[1]+1]~=nil and handCards[v.cards[1]+1]==2) and  (handCards[v.cards[1]+2]~=nil and handCards[v.cards[1]+2]==2)
            if one==true or two==true or three==true then 
                isPeng = false
            end
            if isPeng==true then 
                oneAction.oper_type = def.operType.peng
                oneAction.cards = v.cards
                break
            end
        end
        --吃判断该组吃自己有没有，牌都有就不吃了
        if v.oper_type == def.operType.chi then 
            local isChi = true
            for i=1, 3 do
                if handCards[v.cards[i]] > 0 then 
                    isChi = false
                end
            end
            if isChi==true then 
                oneAction.oper_type = def.operType.chi
                oneAction.cards = v.cards
                break
            end
        end
    end
    local args = { 
        seat_id = seatId,
        oper_type = oneAction.oper_type,      
        oper_cards = oneAction.cards,
    }
    log.info("robot do canOper", args)

    skynet.timeout(100, function()
        func(args)
    end)
end

function robot.ting(seatId, func, outCard)
    local tingArgs = { 
        seat_id = seatId,
        oper_type = def.operType.ting,     
        oper_cards = {},
    }
    --log.info("tingArgs=", tingArgs)
    func(tingArgs)

    local outArgs = { 
        seat_id = seatId,
        oper_type = def.operType.outCard,     
        oper_cards = {outCard},
    }
    --log.info("outArgs=", outArgs)
    skynet.timeout(100, function()
        func(outArgs)
    end)
end

return robot